package org.csu.assemblyroom.controller;

import com.alibaba.fastjson.JSON;
import java.util.List;
import org.csu.assemblyroom.common.CommonResponse;
import org.csu.assemblyroom.entity.Booking;
import org.csu.assemblyroom.service.BookingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 预约信息的增删改查
 */
@Controller
@RequestMapping("/booking")
public class BookingController {
    @Autowired
    private BookingService bookingService;

    @PostMapping("")
    @ResponseBody
    public CommonResponse<Booking> insertBooking(@RequestBody String json){
        Booking booking = JSON.parseObject(json,Booking.class);
        return bookingService.insertBooking(booking);
    }

    @GetMapping("/user/{userId}")
    @ResponseBody
    public CommonResponse<List<Booking>> getBookingByUser(@PathVariable("userId") String userId){
        CommonResponse<List<Booking>> response = bookingService.getBookingByUser(userId);
        if(response.isSuccess()){
            return response;
        }
        return response;
    }

    @GetMapping("/room/{roomNum}")
    @ResponseBody
    public CommonResponse<List<Booking>> getBookingByRoom(@PathVariable("roomNum") String roomNum){
        CommonResponse<List<Booking>> response = bookingService.getBookingByRoom(roomNum);
        if(response.isSuccess()){
            return response;
        }
        return response;
    }

    @GetMapping("/{id}")
    @ResponseBody
    public CommonResponse<Booking> getBookingByBookingId(@PathVariable("id") int bookingId){
        CommonResponse<Booking> response = bookingService.getBookingByBookingId(bookingId);
        if(response.isSuccess()){
            return response;
        }
        return response;
    }

    @GetMapping("")
    @ResponseBody
    public CommonResponse<List<Booking>> getBookingList(){
        return bookingService.getBookingList();
    }

    @PutMapping("")
    @ResponseBody
    public CommonResponse<Booking> updateBooking(@RequestBody String json){
        Booking booking = JSON.parseObject(json,Booking.class);
        return bookingService.updateBooking(booking);
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public CommonResponse<Booking> deleteBooking(@PathVariable("id") int bookingId){
        return bookingService.deleteBooking(bookingId);
    }
}

